import cv2


class Rescale(object):
    """Rescale the image in a sample to a given size.

    Args:
        output_size (tuple or int): Desired output size. If tuple, output is
            matched to output_size. If int, smaller of image edges is matched
            to output_size keeping aspect ratio the same.
    """

    def __init__(self, output_size):
        assert isinstance(output_size, (int, tuple))  # 类型是其中的一种
        self.output_size = output_size

    def __call__(self, sample):  # call函数是什么时候调用的？难道是类名方法？
        image, landmarks = sample['image'], sample['landmarks']

        h, w = image.shape[:2]  # 这种赋值方法对于java，简直太简洁
        if isinstance(self.output_size, int):  # 是一个整数，以较小的数为该,写框架为了适配各种情况，就是要处理这么多代码
            if h > w:
                new_h, new_w = self.output_size * h / w, self.output_size
            else:
                new_h, new_w = self.output_size, self.output_size * w / h
        else:
            new_h, new_w = self.output_size

        new_h, new_w = int(new_h), int(new_w)
        img = cv2.resize(image, (new_h, new_w))  # 调用底层函数实现
        # h and w are swapped for landmarks because for images,
        # x and y axes are axis 1 and 0 respectively
        landmarks = landmarks * [new_w / w, new_h / h]  # 这个点的坐标要压缩
        return {'image': img, 'landmarks': landmarks}


